package com.zg.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zg.pojo.Goods;
import com.zg.pojo.Tag;
import com.zg.service.GoodsService;
import com.zg.service.TagService;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Resource
	private GoodsService goodsService;
	@Resource
	private TagService tagService;
	
	@RequestMapping("/showgoods")
	public String showGoods(HttpServletRequest request,Model model) {
		int id = new Integer(request.getParameter("id"));
		Goods goods = this.goodsService.getGoodsById(id);
		model.addAttribute("goods", goods);
		return "goods/showGoods";
	}
	
	@RequestMapping(value="/addgoods",method=RequestMethod.GET)
	public String addGoodsForm(HttpServletRequest request,Model model) {
		List<Tag> tags;
		tags = this.tagService.getAll();
		model.addAttribute("tags", tags);
		return "goods/addGoods";
	}
	
	@RequestMapping(value="/addgoods", method=RequestMethod.POST)
	public String addGoods(HttpServletRequest request,Model model) {
		String name = new String(request.getParameter("name"));
		int number = new Integer(request.getParameter("number"));
		String video = new String(request.getParameter("video"));
		int price = new Integer(request.getParameter("price"));
		int priceNow = new Integer(request.getParameter("priceNow"));
		int priceVideo = new Integer(request.getParameter("priceVideo"));
		int priceSk = new Integer(request.getParameter("priceSk"));
		
		String upTime = new String(request.getParameter("upTime"));
		String downTime = new String(request.getParameter("downTime"));
		String image1 = new String(request.getParameter("image1"));
		String image2 = new String(request.getParameter("image2"));
		String image3 = new String(request.getParameter("image3"));
		String image4 = new String(request.getParameter("image4"));
		String image5 = new String(request.getParameter("image5"));
		int categorize = new Integer(request.getParameter("categorize"));
		String color = new String(request.getParameter("color"));
		String standard = new String(request.getParameter("standard"));
		int template = new Integer(request.getParameter("template"));
		
		Goods goods = new Goods();
		
		goods.setName(name);
		goods.setNumber(number);
		goods.setVideo(video);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String addDate = new String(dateFormat.format(now)); 
		String modifyDate = new String(addDate);
		goods.setAddDate(addDate);
		goods.setModifyDate(modifyDate);
		goods.setPrice(price);
		goods.setPriceNow(priceNow);
		goods.setPriceVideo(priceVideo);
		goods.setPriceSk(priceSk);
		goods.setUptime(upTime);
		goods.setDowntime(downTime);
		goods.setImages(image1 + ":" + image2 + ":" + image3 + ":" + image4 + ":" + image5);
		goods.setCategorize(categorize);
		goods.setColor(color);
		goods.setStandard(standard);
		goods.setTemplate(template);
		
		this.goodsService.insert(goods);
		
		return "redirect:/goods/listgoods";
	}
	
	@RequestMapping(value="/listgoods")
	public String listGoods(HttpServletRequest request,Model model) {
		List<Goods> goods;
		goods = this.goodsService.getGoodsAll();
		model.addAttribute("list", goods);
		return "goods/listGoods";
	}
	
	@RequestMapping(value="/editgoods", method=RequestMethod.GET)
	public String editGoodsForm(HttpServletRequest request,Model model)
	{
		int id = new Integer(request.getParameter("id"));
		
		Goods goods = new Goods();
		goods = this.goodsService.getGoodsById(id);
		
		model.addAttribute("goods", goods);
		return "goods/editGoods";
	}
	
	@RequestMapping(value="/editgoods", method=RequestMethod.POST)
	public String editGoods(HttpServletRequest request, Model model) {
		int id = new Integer(request.getParameter("id"));
		Goods goods = new Goods();
		goods = this.goodsService.getGoodsById(id);
		
		String name = new String(request.getParameter("name"));
		int number = new Integer(request.getParameter("number"));
		String video = new String(request.getParameter("video"));
		int price = new Integer(request.getParameter("price"));
		int priceNow = new Integer(request.getParameter("priceNow"));
		int priceVideo = new Integer(request.getParameter("priceVideo"));
		int priceSk = new Integer(request.getParameter("priceSk"));
		
		String upTime = new String(request.getParameter("upTime"));
		String downTime = new String(request.getParameter("downTime"));
		String images = new String(request.getParameter("images"));
		int categorize = new Integer(request.getParameter("categorize"));
		String color = new String(request.getParameter("color"));
		String standard = new String(request.getParameter("standard"));
		int template = new Integer(request.getParameter("template"));
		
		
		goods.setName(name);
		goods.setNumber(number);
		goods.setVideo(video);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String modifyDate = new String(dateFormat.format(now)); 
		goods.setModifyDate(modifyDate);
		goods.setPrice(price);
		goods.setPriceNow(priceNow);
		goods.setPriceVideo(priceVideo);
		goods.setPriceSk(priceSk);
		goods.setUptime(upTime);
		goods.setDowntime(downTime);
		goods.setImages(images);
		goods.setCategorize(categorize);
		goods.setColor(color);
		goods.setStandard(standard);
		goods.setTemplate(template);
		
		this.goodsService.updateByPrimaryKeySelective(goods);
		
		return "redirect:/goods/listgoods";
	}
}
